{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7ba17f70-5ba1-4b4c-a916-eb0f96cbf94f",
   "metadata": {},
   "source": [
    "# Tookit\n",
    "tookit是langchain已经封装好的一系列工具，一个工具包是一组工具来组合完成特定的任务"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2349d844-e8cc-47d0-b776-13a0940a48f9",
   "metadata": {},
   "source": [
    "## Azure认知服务 https://portal.azure.com/#allservices\n",
    "- AzureCogsFormRecognizerTool：从文档里提取文本\n",
    "- AzureCogsSpeech2TextTool：语音到文本\n",
    "- AzureCogsText2SpeechTool：文本到语音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d533e8f-326e-4d12-a868-f012306c2ed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install --upgrade azure-ai-formrecognizer > /dev/null\n",
    "! pip install --upgrade azure-cognitiveservices-speech > /dev/null\n",
    "! pip install azure-ai-textanalytics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3a56a70-70b6-4dd3-9335-979268e441b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"AZURE_COGS_KEY\"] = \"c10\"\n",
    "os.environ[\"AZURE_COGS_ENDPOINT\"] = \"https://eastus.api.cognitive.microsoft.com/\"\n",
    "os.environ[\"AZURE_COGS_REGION\"] = \"eastus\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "777accfa-0b9b-4b3d-8e43-1d2e4dbd41c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建toolkit\n",
    "from langchain.agents.agent_toolkits import AzureCognitiveServicesToolkit\n",
    "\n",
    "toolkit = AzureCognitiveServicesToolkit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fbc7e65-4f92-4c91-9836-4624f5c871ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "[tool.name for tool in toolkit.get_tools()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "975b94f8-8287-4281-8861-4ce03618e368",
   "metadata": {},
   "outputs": [],
   "source": [
    "#agent使用\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import initialize_agent, AgentType\n",
    "\n",
    "llm = ChatOpenAI(temperature=0,model=\"gpt-4-1106-preview\")\n",
    "\n",
    "agent = initialize_agent(\n",
    "    tools=toolkit.get_tools(), # 获取的一组工具\n",
    "    llm=llm,\n",
    "    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "378b3b82-bd03-4a43-ad4f-1ed5076f1ceb",
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_file = agent.run(\"Tell me a joke and read it out for me.\")\n",
    "print(audio_file)\n",
    "#from IPython import display\n",
    "\n",
    "#audio = display.Audio(audio_file)\n",
    "#display.display(audio)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7be2009-1e98-44a8-a91d-718093e4af8a",
   "metadata": {},
   "source": [
    "## python代码机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7041516d-9c6f-4332-9062-b47e4e1957a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install langchain_experimental"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a190a94e-6623-42c5-940d-f572dcc6304d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_experimental.agents.agent_toolkits import create_python_agent\n",
    "from langchain_experimental.tools import PythonREPLTool\n",
    "from langchain_experimental.utilities import PythonREPL\n",
    "from langchain.llms.openai import OpenAI\n",
    "from langchain.agents.agent_types import AgentType\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "agent_executor = create_python_agent(\n",
    "    llm=ChatOpenAI(temperature=0, model=\"gpt-4-1106-preview\"),\n",
    "    tool=PythonREPLTool(),\n",
    "    verbose=True,\n",
    "    agent_type=AgentType.OPENAI_FUNCTIONS,\n",
    "    agent_executor_kwargs={\"handle_parsing_errors\": True},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bbb927e-5813-45e8-b41b-f9655d6cb9cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor.run(\"What is the 10th fibonacci number?\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (langchain)",
   "language": "python",
   "name": "langchain-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
